'page_namespace',
'page_title',
'page_id',
- 'page_latest'
+ 'page_latest',
+ 'page_is_redirect',
+ 'page_len',
);
}
return array( 'user_name' );
}
+ /**
+ * Do a batched query to get the parent revision lengths
+ * @param $db DatabaseBase
+ * @param $revIds Array
+ * @return array
+ */
+ public static function getParentLengths( $db, array $revIds ) {
+ $revLens = array();
+ if ( !$revIds ) {
+ return $revLens; // empty
+ }
+ wfProfileIn( __METHOD__ );
+ $res = $db->select( 'revision',
+ array( 'rev_id', 'rev_len' ),
+ array( 'rev_id' => $revIds ),
+ __METHOD__ );
+ foreach ( $res as $row ) {
+ $revLens[$row->rev_id] = $row->rev_len;
+ }
+ wfProfileOut( __METHOD__ );
+ return $revLens;
+ }
+
/**
* Constructor
*
# if we have a content object, use it to set the model and type
if ( !empty( $row['content'] ) ) {
- if ( !empty( $row['text_id'] ) ) { #FIXME: when is that set? test with external store setup! check out insertOn()
+ if ( !empty( $row['text_id'] ) ) { //@todo: when is that set? test with external store setup! check out insertOn() [dk]
throw new MWException( "Text already stored in external store (id {$row['text_id']}), can't serialize content object" );
}
}
$this->mTitle = null; # Load on demand if needed
- $this->mCurrent = false; #XXX: really? we are about to create a revision. it will usually then be the current one.
+ $this->mCurrent = false; # XXX: really? we are about to create a revision. it will usually then be the current one.
# If we still have no length, see it we have the text to figure it out
if ( !$this->mSize ) {
if ( !is_null( $this->mContent ) ) {
$this->mSize = $this->mContent->getSize();
} else {
- #XXX: my be inconsistent with the notion of "size" use for the present content model
- #NOTE: should never happen if we have either text or content object!
- $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText );
+ #NOTE: this should never happen if we have either text or content object!
+ $this->mSize = null;
}
}
throw new MWException( 'Revision constructor passed invalid row format.' );
}
$this->mUnpatrolled = null;
-
- // @TODO: add support for ar_content_format, ar_content_model, rev_content_format, rev_content_model to API
- // @TODO: get rid of $mText
}
/**
* Get revision ID
*
- * @return Integer
+ * @return Integer|null
*/
public function getId() {
return $this->mId;
/**
* Get text row ID
*
- * @return Integer
+ * @return Integer|null
*/
public function getTextId() {
return $this->mTextId;
/**
* Returns the length of the text in this revision, or null if unknown.
*
- * @return Integer
+ * @return Integer|null
*/
public function getSize() {
return $this->mSize;
/**
* Returns the base36 sha1 of the text in this revision, or null if unknown.
*
- * @return String
+ * @return String|null
*/
public function getSha1() {
return $this->mSha1;
}
/**
- * Returns the title of the page associated with this entry.
+ * Returns the title of the page associated with this entry or null.
+ *
+ * Will do a query, when title is not set and id is given.
*
- * @return Title
+ * @return Title|null
*/
public function getTitle() {
if( isset( $this->mTitle ) ) {
return $this->mTitle;
}
- $dbr = wfGetDB( DB_SLAVE );
- $row = $dbr->selectRow(
- array( 'page', 'revision' ),
- self::selectPageFields(),
- array( 'page_id=rev_page',
- 'rev_id' => $this->mId ),
- __METHOD__ );
- if ( $row ) {
- $this->mTitle = Title::newFromRow( $row );
+ if( !is_null( $this->mId ) ) { //rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
+ $dbr = wfGetDB( DB_SLAVE );
+ $row = $dbr->selectRow(
+ array( 'page', 'revision' ),
+ self::selectPageFields(),
+ array( 'page_id=rev_page',
+ 'rev_id' => $this->mId ),
+ __METHOD__ );
+ if ( $row ) {
+ $this->mTitle = Title::newFromRow( $row );
+ }
}
+
+ //@todo: as a last resort, perhaps load from page table, if $this->mPage is given?!
return $this->mTitle;
}
/**
* Get the page ID
*
- * @return Integer
+ * @return Integer|null
*/
public function getPage() {
return $this->mPage;
*
* @param $audience Integer: one of:
* Revision::FOR_PUBLIC to be displayed to all users
- * Revision::FOR_THIS_USER to be displayed to $wgUser
+ * Revision::FOR_THIS_USER to be displayed to the given user
* Revision::RAW get the ID regardless of permissions
* @param $user User object to check for, only if FOR_THIS_USER is passed
* to the $audience parameter
*
* @param $audience Integer: one of:
* Revision::FOR_PUBLIC to be displayed to all users
- * Revision::FOR_THIS_USER to be displayed to $wgUser
+ * Revision::FOR_THIS_USER to be displayed to the given user
* Revision::RAW get the text regardless of permissions
* @param $user User object to check for, only if FOR_THIS_USER is passed
* to the $audience parameter
*
* @param $audience Integer: one of:
* Revision::FOR_PUBLIC to be displayed to all users
- * Revision::FOR_THIS_USER to be displayed to $wgUser
+ * Revision::FOR_THIS_USER to be displayed to the given user
* Revision::RAW get the text regardless of permissions
* @param $user User object to check for, only if FOR_THIS_USER is passed
* to the $audience parameter
*
* @param $audience Integer: one of:
* Revision::FOR_PUBLIC to be displayed to all users
- * Revision::FOR_THIS_USER to be displayed to $wgUser
+ * Revision::FOR_THIS_USER to be displayed to the given user
* Revision::RAW get the text regardless of permissions
* @param $user User object to check for, only if FOR_THIS_USER is passed
* to the $audience parameter
* @return String
* @deprecated in 1.WD, use getContent() instead
+ * @todo: replace usage in core
*/
- public function getText( $audience = self::FOR_PUBLIC, User $user = null ) { #FIXME: deprecated, replace usage! #FIXME: used a LOT!
+ public function getText( $audience = self::FOR_PUBLIC, User $user = null ) {
wfDeprecated( __METHOD__, '1.WD' );
- $content = $this->getContent();
+ $content = $this->getContent( $audience, $user );
return ContentHandler::getContentText( $content ); # returns the raw content text, if applicable
}
* Fetch revision text without regard for view restrictions
*
* @return String
+ *
+ * @deprecated since 1.WD. Instead, use Revision::getContent( Revision::RAW ) or Revision::getSerializedData() as appropriate.
*/
- public function getRawText() { #FIXME: deprecated, replace usage!
+ public function getRawText() {
+ wfDeprecated( __METHOD__, "1.WD" );
+
return $this->getText( self::RAW );
}
+ /**
+ * Fetch original serialized data without regard for view restrictions
+ *
+ * @return String
+ *
+ * @since 1.WD
+ */
+ public function getSerializedData() {
+ return $this->mText;
+ }
+
protected function getContentInternal() {
if( is_null( $this->mContent ) ) {
// Revision is immutable. Load on demand:
$model = $this->getContentModel();
$this->mContentHandler = ContentHandler::getForModelID( $model );
- assert( $this->mContentHandler->isSupportedFormat( $this->getContentFormat() ) );
+ $format = $this->getContentFormat();
+
+ if ( !$this->mContentHandler->isSupportedFormat( $format ) ) {
+ $formatName = ContentHandler::getContentFormatMimeType( $format );
+ $modelName = ContentHandler::getContentModelName( $model );
+
+ throw new MWException( "Oops, the content format #$format ($formatName) is not supported for this content model, #$model ($modelName)" );
+ }
}
return $this->mContentHandler;
$row[ 'rev_content_format' ] = $this->getContentFormat();
}
+ $this->checkContentModel();
+
$dbw->insert( 'revision', $row, __METHOD__ );
$this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
return $this->mId;
}
+ protected function checkContentModel() {
+ global $wgContentHandlerUseDB;
+
+ $title = $this->getTitle(); //note: returns null for revisions that have not yet been inserted.
+
+ $model = $this->getContentModel();
+ $format = $this->getContentFormat();
+ $handler = $this->getContentHandler();
+
+ if ( !$handler->isSupportedFormat( $format ) ) {
+ $t = $title->getPrefixedDBkey();
+ $modelName = ContentHandler::getContentModelName( $model );
+ $formatName = ContentHandler::getContentFormatMimeType( $format );
+
+ throw new MWException( "Can't use format #$format ($formatName) with content model #$model ($modelName) on $t" );
+ }
+
+ if ( !$wgContentHandlerUseDB && $title ) {
+ // if $wgContentHandlerUseDB is not set, all revisions must use the default content model and format.
+
+ $defaultModel = ContentHandler::getDefaultModelFor( $title );
+ $defaultHandler = ContentHandler::getForModelID( $defaultModel );
+ $defaultFormat = $defaultHandler->getDefaultFormat();
+
+ if ( $this->getContentModel() != $defaultModel ) {
+ $defaultModelName = ContentHandler::getContentModelName( $defaultModel );
+ $modelName = ContentHandler::getContentModelName( $model );
+ $t = $title->getPrefixedDBkey();
+
+ throw new MWException( "Can't save non-default content model with \$wgContentHandlerUseDB disabled: model is #$model ($modelName), default for $t is #$defaultModel ($defaultModelName)" );
+ }
+
+ if ( $this->getContentFormat() != $defaultFormat ) {
+ $defaultFormatName = ContentHandler::getContentFormatMimeType( $defaultFormat );
+ $formatName = ContentHandler::getContentFormatMimeType( $format );
+ $t = $title->getPrefixedDBkey();
+
+ throw new MWException( "Can't use non-default content format with \$wgContentHandlerUseDB disabled: format is #$format ($formatName), default for $t is #$defaultFormat ($defaultFormatName)" );
+ }
+ }
+
+ $content = $this->getContent( Revision::RAW );
+
+ if ( !$content->isValid() ) {
+ $t = $title->getPrefixedDBkey();
+ $modelName = ContentHandler::getContentModelName( $model );
+
+ throw new MWException( "Content of $t is not valid! Content model is #$model ($modelName)" );
+ }
+ }
+
/**
* Get the base 36 SHA-1 value for a string of text
* @param $text String
wfProfileIn( __METHOD__ );
- $fields = array( 'page_latest', 'rev_text_id', 'rev_len', 'rev_sha1' );
+ $fields = array( 'page_latest', 'page_namespace', 'page_title',
+ 'rev_text_id', 'rev_len', 'rev_sha1' );
if ( $wgContentHandlerUseDB ) {
$fields[] = 'rev_content_model';
'text_id' => $current->rev_text_id,
'parent_id' => $current->page_latest,
'len' => $current->rev_len,
- 'sha1' => $current->rev_sha1,
+ 'sha1' => $current->rev_sha1
);
if ( $wgContentHandlerUseDB ) {
}
$revision = new Revision( $row );
+ $revision->setTitle( Title::makeTitle( $current->page_namespace, $current->page_title ) );
} else {
$revision = null;
}